package com.kj.tms.common.utils.calx.sheet;


 import com.kj.tms.common.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Pattern;

/**
 * @ProjectName: ledger
 * @Package: com.hongtu.tms.ledger.calx.factory.sheet
 * @ClassName: Time
 * @Description: []
 * @Author: [zhuyongjie]
 * @CreateDate: 2018/4/18 14:40
 * @UpdateUser: []
 * @UpdateDate: 2018/4/18 14:40
 * @UpdateRemark: []
 * @Version: 1.0
 * Copyright: 北京宏图天安 Copyright (c) 2018
 */
public class Time {
    //12小时制hh:mm:ss [am|pm]
    public static String regex = "^([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm)";
    //24小时制hh:mm:ss
    public static String regex2 = "^([0]?[0-9]|1[0-9]|2[0-3])[:][0-5][0-9]([:][0-5][0-9])?";

    /**
     * @Method      time
     * @Param		time
     * @Return      java.util.Date
     * @Exception
     * @Description [日期字符串转换为日期]
     * @Author      zhuyongjie
     * @Version     1.0
     * @Date        2018/4/18 14:55
     */
    public static Calendar time(Object time){
        String timeStr = StringUtil.toString(time);
        boolean m1 = Pattern.matches(regex, timeStr);
        boolean m2 = Pattern.matches(regex2, timeStr);
        //当前日期
        Calendar today = Calendar.getInstance();
        if(m1 || m2){
            String[] split = timeStr.split(":");
            String hour = StringUtil.isEmptyOrNull(split[0]) ? "0" : split[0];
            String minute = StringUtil.isEmptyOrNull(split[1]) ? "0" : split[1];
            String second = StringUtil.isEmptyOrNull(split[2]) ? "0" : split[2];
            if(m1){
                second = second.replaceAll("(AM|am|aM|Am|PM|pm|pM|Pm)","").replaceAll("\\s+","");//替换空格和am,pm
            }
            if(m1){
                today.set(Calendar.HOUR,StringUtil.toInt(hour));
            }else if(m2){
                today.set(Calendar.HOUR_OF_DAY,StringUtil.toInt(hour));
            }

            today.set(Calendar.MINUTE,StringUtil.toInt(minute));
            today.set(Calendar.SECOND,StringUtil.toInt(second));
            return today;

        }else{
            return null;
        }
    }//time end

    public static void main(String[] args) {
        Calendar c = time("03:03:32pm");
        //获取年
        int year = c.get(Calendar.YEAR);
        //获取月份，0表示1月份
        int month = c.get(Calendar.MONTH) + 1;
        //获取当前天数
        int day = c.get(Calendar.DAY_OF_MONTH);
        //获取本月最小天数
        int first = c.getActualMinimum(Calendar.DAY_OF_MONTH);
        //获取本月最大天数
        int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //获取当前小时
        int time = c.get(Calendar.HOUR_OF_DAY);
        //获取当前分钟
        int min = c.get(Calendar.MINUTE);
        //获取当前秒
        int sec = c.get(Calendar.SECOND);

        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String curDate = s.format(c.getTime());                                      //当前日期
        System.out.println("当前时间：" + year + "-" + month + "-" + day + " " + time + ":" + min + ":" + sec);
        System.out.println("第一天和最后天：" + first +"," + last);
        System.out.println("当前日期：" + curDate);
    }
}
